﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace LayoutLoggingService.Client
{
    /// <summary>
    /// Used to build service requests from a file.
    /// </summary>
    public class FileRequestBuilder
    {
        public static ArrayList GetFolderName(string floder)
        {
            ArrayList list = new ArrayList();

            DirectoryInfo dir = new DirectoryInfo(floder);
            foreach (DirectoryInfo dChild in dir.GetDirectories())
            {
                list.Add(dChild.Name);
            }

            return list;
        }

        public static ArrayList GetRequestList(string folderPath)
        {
            ArrayList list = new ArrayList();

            DirectoryInfo dir = new DirectoryInfo(folderPath);
            if (dir.Exists)
            {
                foreach (FileInfo testCaseFile in dir.GetFiles("*.xml"))
                {
                    list.Add(testCaseFile.Name);
                }
            }

            return list;
        }

        public static Request GetRequestInfo(string filePath)
        {
            Request request = new Request();

            if (File.Exists(filePath))
            {
                using (StreamReader reader = new StreamReader(filePath))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        Match mError = _extractErrorTextFromPayloadFileRegex.Match(line);
                        if (mError.Success)
                        {
                            request.ErrMessage = mError.Groups[1].Value.Trim();
                        }
                        Match mDefalutUrl = _extractDefalutUrlFromPayloadFileRegex.Match(line);
                        if (mDefalutUrl.Success)
                        {
                            request.DefaultUrl = mDefalutUrl.Groups[1].Value.Trim();
                        }

                        request.Payload += line;
                        request.Payload += Environment.NewLine;
                    }
                }
            }

            return request;
        }

        // Extracts the error from XML comment in the file
        // (since XML may be malformed, cannot use XML parser and using regular expressions instead
        static readonly Regex _extractDefalutUrlFromPayloadFileRegex = new Regex(@"<!\-\- DEFAULTURL\s*:(.*)\-\->");
        static readonly Regex _extractErrorTextFromPayloadFileRegex = new Regex(@"<!\-\- ERROR\s*:(.*)\-\->");
        private static string ExtractErrorTextFromPayloadFile(string filePath)
        {
            string errorText = null;
            using (StreamReader reader = new StreamReader(filePath))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    Match m = _extractErrorTextFromPayloadFileRegex.Match(line);
                    if (m.Success)
                    {
                        errorText = m.Groups[1].Value.Trim();
                        break;
                    }
                }
            }
            return errorText;
        }

        public static string ExtractErrorTextFromRequestBody(string requestBody)
        {
            string errorText = null;
            Match m = _extractErrorTextFromPayloadFileRegex.Match(requestBody);
            if (m.Success)
            {
                errorText = m.Groups[1].Value.Trim();
            }
            return errorText;
        }

        public static string ExtractDefalutUrlFromRequestBody(string requestBody)
        {
            string defalutUrl = null;
            Match m = _extractDefalutUrlFromPayloadFileRegex.Match(requestBody);
            if (m.Success)
            {
                defalutUrl = m.Groups[1].Value.Trim();
            }
            return defalutUrl;
        }
    }
}
